package com.qf.spring.aop.proxy.dynamic;

import com.qf.spring.aop.service.OrderService;
import com.qf.spring.aop.service.UserService;
import com.qf.spring.aop.service.impl.OrderServiceImpl;
import com.qf.spring.aop.service.impl.UserServiceImpl;

import java.lang.reflect.Proxy;

/**
 * 测试动态代理
 */
public class Test {
    public static void main(String[] args) {

        //用户业务逻辑日志增强
        //1.创建目标对象
        UserService target1 = new UserServiceImpl();
        //2.创建方法增强动态代理对象
        ProxyHandler p1 = new ProxyHandler(target1);
        //3.通过动态代理获取业务代理对象
        UserService userServiceProxy = (UserService) Proxy.newProxyInstance(target1.getClass().getClassLoader(),
                target1.getClass().getInterfaces(),
                p1);

        //4.调用业务方法
        userServiceProxy.login();
        System.out.println("---------");
        userServiceProxy.loginOut();



        //订单业务逻辑日志增强
        //创建目标对象
        OrderService orderServiceTarget = new OrderServiceImpl();
        //创建方法增强动态代理对象
        ProxyHandler orderServiceProxyHandler = new ProxyHandler(orderServiceTarget);
        //通过动态代理获得业务代理对象
        OrderService orderServiceProxy = (OrderService) Proxy.newProxyInstance(orderServiceTarget.getClass().getClassLoader(),
                orderServiceTarget.getClass().getInterfaces(),
                orderServiceProxyHandler);
        //调用业务方法
        System.out.println("==========");
        orderServiceProxy.orders();
        System.out.println(orderServiceProxy.getClass());
    }
}
